package com.shujia.spark.streaming

import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.streaming.{Durations, StreamingContext}

object Demo1WordCount {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setMaster("local[2]")
    conf.setAppName("wc")
    val sc = new SparkContext(conf)

    //1、创建spark streaming环境
    //指定处理的间隔时间
    val ssc = new StreamingContext(sc, Durations.seconds(5))


    //2、读取数据
    //nc -lk 8888
    //yum install nc
    val linesDS: ReceiverInputDStream[String] = ssc.socketTextStream("master", 8888)


    //统计单词的数量
    val countDS: DStream[(String, Int)] = linesDS
      .flatMap(_.split(","))
      .map((_, 1))
      .reduceByKey(_ + _)

    countDS.print()


    //启动spark streaming程序
    ssc.start()
    ssc.awaitTermination()
  }
}
